<?php 
namespace Think;
class Think{
	//类映射
	private static $_map  = array();
	//实例化对像 
	private static $_instance = array();
	
	static public function start()
	{
		spl_autoload_register('Think\Think::autoload');
		
		Storage::connect(STORAGE_TYPE);  
		
		$runtimefile = RUNTIME_PATH .  APP_MODE . '~runtime.php';
		if (!APP_DEBUG && Storage::has($runtimefile)) 
		{
		echo $runtimefile;
		    Storage::load($runtimefile);
		}else{
			if (Storage::has($runtimefile)) 
			    Storage::unlink($runtimefile);
			
				$mode = include is_file(CONF_PATH . 'core.php') ? CONF_PATH . 'core.php' : MODE_PATH . APP_MODE . '.php';
				
				//加载核心库
				foreach ($mode['core'] as $file) {
					if (is_file($file)) {
					    include $file;
					}
				}
				
				//加载应用模式配置文件
				foreach ($mode['config'] as $key => $file) {
					is_numeric($key) ? C(load_config($file)) : C($key,load_config($file));
				}
				
				// 读取当前应用模式对应的配置文件
				if ('common' != APP_MODE AND is_file(CONF_PATH . 'config_' . APP_MODE . CONF_EXT)) 
				{
					C(load_config(CONF_PATH . 'config_' . APP_MODE . CONF_EXT));
				}
				
				// 加载应用别名定义文件
				if (is_file(CONF_PATH . 'alias.php')) 
				{
				    self::addMap(include CONF_PATH . 'alias.php');
				}
				
				// 加载模式行为定义
				
				
				
		}
		
		
		
		
	}


	static private function addMap($class,$map='')
	{
		if (is_array($class)) 
		{
		    self::$_map = array_merge(self::$_map,$class);
		}else{
			self::$_map[$class] = $map;
		}
	}
	
	static public function autoload($class)
	{
		
		if (FALSE  !== strpos($class, '\\')) 
		{
		    $name = strstr($class, '\\',TRUE);
			if (in_array($name, array('Think','Org','Behavior','Com','Vendor')) or is_dir(LIB_PATH . $name)) 
			{
				$path = LIB_PATH;
			}
			
			$filename = $path . str_replace('\\', '/', $class) . EXT;
			
			if (is_file($filename)) 
			{
				if (IS_WIN && FALSE === strpos(str_replace('/', '\\', realpath($filename)), $class . EXT)){
					return;
				}
				include $filename;
			}
			
		}
	}
	
}
